package io.renren.modules.car.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.car.entity.CarBaseEntity;
import io.renren.modules.car.service.CarBaseService;
import io.renren.modules.car.service.CarShopMoneyService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 车辆基本信息
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-09-17 11:28:03
 */
@RestController
@RequestMapping("car/carbase")
public class CarBaseController extends AbstractController {
    @Autowired
    private CarBaseService carBaseService;
    @Resource
    private CarShopMoneyService carShopMoneyService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("car:carbase:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("create_user",getUserId());
        PageUtils page = carBaseService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("car:carbase:info")
    public R info(@PathVariable("id") Integer id){
        CarBaseEntity carBase = carBaseService.getMsgById(id);

        return R.ok().put("carBase", carBase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("car:carbase:save")
    public R save(@RequestBody CarBaseEntity carBase){
        carBase.setCreateTime(new Date());
        carBase.setUpdateTime(new Date());
        carBase.setCreateUser(getUserIdInt());
        boolean b = carBaseService.saveWithFriendShop(carBase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("car:carbase:update")
    public R update(@RequestBody CarBaseEntity carBase){
        ValidatorUtils.validateEntity(carBase);
        carBase.setUpdateTime(new Date());
        carBase.setCreateUser(getUserIdInt());
        boolean b = carBaseService.updateByIdWithFriendShop(carBase);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("car:carbase:delete")
    public R delete(@RequestBody Integer[] ids){
        carBaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
